package com.lhy.gulimall.client2.controller;

import cn.hutool.http.HttpUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class HelloController {

    @Value("${sso.server.url}")
    private String url;

    @GetMapping("/hello")
    public String hello() {

        return "hello";
    }

    @GetMapping("/enter")
    public String enter(Model model, HttpSession httpSession, @RequestParam(value = "token",required = false) String token) {
        if(!StringUtils.isEmpty(token)){
            // 去sso 通过token获取用户信息
            String user = HttpUtil.get("http://sso.com:8080/userInfo?token=" + token);
            httpSession.setAttribute("loginUser",user);
        }
        Object loginUser = httpSession.getAttribute("loginUser");
        if (loginUser != null) {
            List<String> lists = new ArrayList<>();
            lists.add("张三");
            lists.add("李四");
            model.addAttribute("lists", lists);
            model.addAttribute("loginUser", loginUser);
            return "list";
        }
        return "redirect:" + url + "?redirect_url=http://client2.com:8082/enter";
    }

}
